﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Model;
using DataAccess;

namespace Project
{
    /// <summary>
    /// Lógica de interacción para LookForFunctionary.xaml
    /// </summary>
    public partial class LookForFunctionary : Window
    {
        List<Functionary> allEemployees;
        public Functionary employee;
        public event RoutedEventHandler getEmployee;
        public class Information
        {
            public long Ci { get; set; }
            public string Name { get; set; }
            public string Charge { get; set; }
            public string Grade { get; set; }
        }

        public LookForFunctionary()
        {
            InitializeComponent();
            allEemployees = new List<Functionary>();
            allEemployees = DataBaseAccess.GetAllemployees;
            for (int i = 0; i < allEemployees.Count; i++)
            {
                string maternal;
                if (allEemployees[i].MaternalName == null)
                    maternal = " ";
                else
                    maternal = allEemployees[i].MaternalName;
                ListEmployees.Items.Add("CI: " + allEemployees[i].CI + " Nombre: " + allEemployees[i].Name + " " + allEemployees[i].PaternalName + " " + maternal + " Cargo: " + allEemployees[i].WorkingRole + " Rango: " + allEemployees[i].Rank);
            }
        }

        public bool containsString(string cad1, string cad2)
        {
            if (cad1 != null)
            {
                for (int i = 0; i < cad2.Length; i++)
                {
                    if (cad1[i] != cad2[i])
                        return false;
                }
                return true;
            }
            else
                return false;
        }
        
        public void fillList()
        {
            string maternal;
            if (!String.IsNullOrEmpty(tbCi.Text))
            {
                if (!String.IsNullOrEmpty(tbName.Text))
                {
                    for (int i = 0; i < allEemployees.Count; i++)
                    {
                        if (containsString(allEemployees[i].Name, tbName.Text) || containsString(allEemployees[i].PaternalName, tbName.Text) || containsString(allEemployees[i].MaternalName, tbName.Text) && containsString(allEemployees[i].CI.ToString(), tbCi.Text))
                        {
                            if (allEemployees[i].MaternalName == null)
                                maternal = " ";
                            else
                                maternal = allEemployees[i].MaternalName;
                            ListEmployees.Items.Add("CI: " + allEemployees[i].CI + " Nombre: " + allEemployees[i].Name + " " + allEemployees[i].PaternalName + " " + maternal + " Cargo: " + allEemployees[i].WorkingRole + " Rango: " + allEemployees[i].Rank);
                        }
                    }
                }
                else
                {
                    for (int i = 0; i < allEemployees.Count; i++)
                    {
                        if (containsString(allEemployees[i].CI.ToString(), tbCi.Text))
                        {
                            
                            if (allEemployees[i].MaternalName == null)
                                maternal = " ";
                            else
                                maternal = allEemployees[i].MaternalName;
                            ListEmployees.Items.Add("CI: " + allEemployees[i].CI + " Nombre: " + allEemployees[i].Name + " " + allEemployees[i].PaternalName + " " + maternal + " Cargo: " + allEemployees[i].WorkingRole + " Rango: " + allEemployees[i].Rank);
                        }
                    }
                }
            }
            else
            {
                if (!String.IsNullOrEmpty(tbName.Text))
                {
                    for (int i = 0; i < allEemployees.Count; i++)
                    {
                        if (containsString(allEemployees[i].Name, tbName.Text) || containsString(allEemployees[i].PaternalName, tbName.Text) || containsString(allEemployees[i].MaternalName, tbName.Text))
                        {
                            if (allEemployees[i].MaternalName == null)
                                maternal = " ";
                            else
                                maternal = allEemployees[i].MaternalName;
                            ListEmployees.Items.Add("CI: " + allEemployees[i].CI + " Nombre: " + allEemployees[i].Name + " " + allEemployees[i].PaternalName + " " + maternal + " Cargo: " + allEemployees[i].WorkingRole + " Rango: " + allEemployees[i].Rank);
                        }
                    }
                }
            }
        }

        private void tbCi_TextChanged(object sender, TextChangedEventArgs e)
        {
            ListEmployees.Items.Clear();
            fillList();
        }

        private void tbName_TextChanged(object sender, TextChangedEventArgs e)
        {
            ListEmployees.Items.Clear();
            fillList();
        }

        public void findEmployee()
        {
            try
            {
                string[] words = ListEmployees.SelectedItem.ToString().Split(' ');
                for (int i = 0; i < allEemployees.Count; i++)
                {
                    if (allEemployees[i].CI.ToString() == words[1])
                    {
                        employee = allEemployees[i];
                        i = allEemployees.Count;
                    }
                }
            }
            catch
            {
                MessageBox.Show("no selecciono un empleado");
            }
        }

        private void ListEmployees_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            findEmployee();
            getEmployee(sender, e);
        }

            /*private void MenuItem2_Click(object sender, RoutedEventArgs e)
            {
                Content.Children.Clear();
                Content.Children.Add(new ViewFunctionary());
            }

            private void MenuItem_Click(object sender, RoutedEventArgs e)
            {
                Content.Children.Clear();
                Content.Children.Add(new ModifyFunctionaryRegister());
            }*/
    }
}